/**
 * Created by zxw1992513@qq.com on 2016/10/19.
 */
var msg = new Object()
var info = new Object()
var orderId  = $("input[name='order-id']").val()
$(document).ready(function(){
    pushTaocan()//渲染套餐数据
    pushApp()//渲染投放app数据
    SuM()
})

function pushTaocan(){
    if(sessionStorage['taocan' + orderId] !=undefined && sessionStorage['taocan' + orderId] !='{}'){
        msg = JSON.parse(sessionStorage['taocan' + orderId])
        for(x in msg){
            if(msg[x].num != 0){
                addDom(x,msg[x].name,msg[x].pri*msg[x].num,msg[x].num,msg[x].num*msg[x].show) //生成购物车taocanDOM
            }
        }
    }
}
function pushApp(){
    if(sessionStorage['app'+orderId] != undefined && sessionStorage['app'+orderId] != '{}'){
        info = JSON.parse(sessionStorage['app'+orderId])
        for(x in info){
            var item = $('.app-item').filter(function(){
                return $(this).attr('data-media-id') == x
            })
            var cpm = item.attr('data-cpm')
            item.addClass('yellow')
            item.find('.app-buy').addClass('full').attr('data-num',info[x].num)
                .attr('data-pri',info[x].num*cpm).attr('data-attr',info[x].num)
                .attr('data-name',info[x].name)
            addAppDom(x,info[x].name,info[x].pri*info[x].num,info[x].num,info[x].num*0.1)
        }
    }
}
function addDom(id,str,appPri,appNum,cpm){
    //往购物车中添加该app信息
    var dom = $('.cart-item-list>.cart-item:first-child').clone(true,true).appendTo('.cart-item-list')
        .attr('item-id',id).attr('item-name',str)
    dom.find('.item-name').text(str)
    dom.find('.item-price').text(appPri)
    dom.find('.item-show').text(cpm)
    dom.find('input').val(appNum)
}
function addAppDom(id,str,appPri,appNum,cpm){
    //往购物车中添加该app信息
    var dom = $('.cart-item-list>.cart-item:first-child').clone(true,true).appendTo('.cart-item-list')
        .attr('app-id',id).attr('app-name',str)
    dom.find('.item-name').text(str)
    dom.find('.item-price').text(appPri)
    dom.find('.item-show').text(cpm.toFixed(1))
    dom.find('input').val(appNum)
}
function SuM(){
    //套餐数据求和
    var sum = 0
    var Pri = 0
    var Cpm = 0
    for(x in msg){
        sum += parseInt(msg[x].num,10)
        Pri += msg[x].num*msg[x].pri
        Cpm += msg[x].num*msg[x].show
    }
    //
    var input = $('button.app-buy').map(function(){
        return $(this).attr('data-num')
    }).get().sum()
    var pushnumber = parseInt((0.1*input).toFixed(1),10) + Cpm
    var price = $('button.app-buy').map(function(){
            return $(this).attr('data-pri')
        }).get().sum() + Pri
    $('.price span').text(price)
    input += sum
    if(input != 0){
        $('.shopcart').attr('data-attr',input).addClass('full').removeClass('none')
        $('#pushButton').removeClass('none')
    }else{
        $('.shopcart').attr('data-attr','').removeClass('full').addClass('none')
        $('#pushButton').addClass('none')
    }
    $('.show-number').text(pushnumber)
    sessionStorage['app'+orderId] = JSON.stringify(info);
}
